package com.logos.ruralrevitalization.tools;

import com.logos.ruralrevitalization.converter.AgriculturalProductConverter;
import com.logos.ruralrevitalization.converter.AuthUserConverter;
import com.logos.ruralrevitalization.entity.AgriculturalProduct;
import com.logos.ruralrevitalization.entity.AuthUser;
import com.logos.ruralrevitalization.enums.IsDeleteFlagEnum;
import com.logos.ruralrevitalization.mapper.AgriculturalProductMapper;
import com.logos.ruralrevitalization.req.ProductOrderReq;
import com.logos.ruralrevitalization.req.ShoppingCartReq;
import com.logos.ruralrevitalization.req.chat.AgriculturalProductQuery;
import com.logos.ruralrevitalization.req.chat.ProductOrderQuery;
import com.logos.ruralrevitalization.resp.AgriculturalProductResp;
import com.logos.ruralrevitalization.resp.AuthUserResp;
import com.logos.ruralrevitalization.service.AgriculturalProductService;
import com.logos.ruralrevitalization.service.AuthUserService;
import com.logos.ruralrevitalization.service.ProductOrderService;
import com.logos.ruralrevitalization.service.ShoppingCartService;
import com.logos.ruralrevitalization.utils.RuralLoginUtil;
import com.logos.ruralrevitalization.utils.TouristLoginUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;


/**
 * 游客工具类
 */
@Component
public class TouristTools {

    @Resource
    private AgriculturalProductMapper agriculturalProductMapper;

    @Resource
    private AuthUserService authUserService;

    @Resource
    private ProductOrderService productOrderService;

    @Resource
    private ShoppingCartService shoppingCartService;

    @Resource
    private TouristLoginUtil touristLoginUtil;


    @Tool(description = "查询当前游客个人信息")
    public AuthUserResp queryInfo() {
        AuthUser authUser = authUserService.queryById(Long.valueOf(touristLoginUtil.getId()));
        return AuthUserConverter.INSTANCE.convertEntityToResp(authUser);
    }



    @Tool(description = "根据条件查询农产品")
    public List<AgriculturalProductResp> queryAgriculturalProduct(@ToolParam(required = false, description = "农产品查询条件") AgriculturalProductQuery agriculturalProductQuery) {
        AgriculturalProduct agriculturalProduct = AgriculturalProductConverter.INSTANCE.convertQueryToEntity(agriculturalProductQuery);

        List<AgriculturalProduct> agriculturalProductList = agriculturalProductMapper.queryByConditionWithChat(agriculturalProduct, agriculturalProductQuery.getSorts());
        if (agriculturalProductList == null || agriculturalProductList.isEmpty()) {
            return null;
        }
        List<AgriculturalProductResp> agriculturalProductRespList = new LinkedList<>();
        for (AgriculturalProduct product : agriculturalProductList) {
            AgriculturalProductResp agriculturalProductResp = AgriculturalProductConverter.INSTANCE.convertEntityToResp(product);
            agriculturalProductRespList.add(agriculturalProductResp);
        }
        return agriculturalProductRespList;
    }


    @Tool(description = "将农产品加入购物车，返回true表示加入购物车成功，false表示加入购物车失败")
    public Boolean addShoppingCart(@ToolParam(description = "农产品id") Long productId,
                                   @ToolParam(description = "数量") Integer quantity) {
        ShoppingCartReq shoppingCartReq = new ShoppingCartReq();
        shoppingCartReq.setProductId(productId);
        shoppingCartReq.setQuantity(quantity);
        shoppingCartReq.setUserId(Long.valueOf(touristLoginUtil.getId()));
        if (shoppingCartService.add(shoppingCartReq)) {
            return true;
        }
        return false;
    }

    @Tool(description = "查询购物车")
    public List<AgriculturalProductResp> queryShoppingCart() {
        String id = touristLoginUtil.getId();
        if (StringUtils.isBlank(id)) {
            return null;
        }
        List<AgriculturalProductResp> agriculturalProductRespList = shoppingCartService.queryByUserId(Long.valueOf(id));
        return agriculturalProductRespList;
    }


    @Tool(description = "下单,返回true表示下单成功，false表示下单失败")
    public Boolean orderAgriculturalProduct(@ToolParam(description = "下单的参数")ProductOrderQuery productOrderQuery) {
        ProductOrderReq productOrderReq = new ProductOrderReq();
        productOrderReq.setTotalAmount(productOrderQuery.getTotalAmount());
        productOrderReq.setCustomerId(Long.valueOf(touristLoginUtil.getId()));
        productOrderReq.setShoppingCartIds(productOrderQuery.getShoppingCartIds());
        Boolean result = productOrderService.add(productOrderReq);
        return result;
    }

}
